package com.fourth.service.impl;

import com.fourth.dto.Result;
import com.fourth.enums.ResultCode;
import com.fourth.service.SmsService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import static com.fourth.util.Constants.*;

/**
 * 发送短信的接口
 *
 * @author 邓建波
 */
@Slf4j
@Service
public class SmsServiceImpl implements SmsService {

    /**
     * 腾讯云账户密钥Id
     */
    @Value("${sms.tencent.secretId}")
    private String secretId;

    /**
     * 腾讯云账户密钥 key
     */
    @Value("${sms.tencent.secretKey}")
    private String secretKey;

    /**
     * 地域信息
     */
    @Value("${sms.tencent.regionId}")
    private String regionId;
    /**
     * 签名
     */
    @Value("${sms.tencent.signName}")
    private String signName;

    /**
     * 模板ID
     */
    @Value("${sms.tencent.templateId}")
    private String templateId;
    /**
     * 接入域名
     */
    @Value("${sms.tencent.endpoint}")
    private String endpoint;
    /**
     * sdkAppId
     */
    @Value("${sms.tencent.sdkAppId}")
    private String sdkAppId;

    @Override
    public Result sendSms(String phone, String code) {
        try{
            // TODO 1.创建客户端
            final SmsClient smsClient = createSmsClient();
            // TODO 2.创建请求
            final SendSmsRequest request = createSendSmsRequest();
            // TODO 3.设置将要发送验证码
            String[] templateParamSet = {code};
            request.setTemplateParamSet(templateParamSet);
            // TODO 4.设置将要发送手机号
            String[] phoneNumberSet = {phone};
            request.setPhoneNumberSet(phoneNumberSet);
            // TODO 5.发送请求给腾讯云平台，拿到Response
            final SendSmsResponse response= smsClient.SendSms(request);
            // TODO 6.判断是否发送成功
            final SendStatus[] sendStatusSet = response.getSendStatusSet();
            if(!ACCESS_CODE.equals(sendStatusSet[0].getCode())){
                log.debug("腾讯云平台出错");
                return new Result(ResultCode.TENCENT_SMS_ERROR);
            }
        }catch (TencentCloudSDKException e){
            // TODO 异常处理
            e.printStackTrace();
            log.warn("腾讯云平台出错");
            return new Result(ResultCode.TENCENT_SMS_ERROR);
        }
        // TODO 发送成功
        return Result.ok("短信验证发送成功");
    }

    /**
     * 创建请求
     * @return SendSmsRequest
     */
    private SendSmsRequest createSendSmsRequest(){
        final SendSmsRequest sendSmsRequest = new SendSmsRequest();
        // TODO 1.设置sdkAppId
        sendSmsRequest.setSmsSdkAppId(sdkAppId);
        // TODO 2.设置签名
        sendSmsRequest.setSignName(signName);
        // TODO 3.设置模板ID
        sendSmsRequest.setTemplateId(templateId);
        return sendSmsRequest;
    }

    /**
     * 创建客户端
     * @return SmsClient
     */
    private SmsClient createSmsClient(){
        // TODO 1.设置密钥对
        Credential credential = new Credential(secretId,secretKey);

        HttpProfile httpProfile = new HttpProfile();
        // TODO 2.设置接入域名
        httpProfile.setEndpoint(endpoint);
        ClientProfile clientProfile = new ClientProfile();
        // TODO 3. 设置httpProfile
        clientProfile.setHttpProfile(httpProfile);
        return new SmsClient(credential,regionId,clientProfile);
    }


}
